let app = getApp()
const util = require('../../utils/util.js')
const { getStuQuestionnaire } = require('../../api/geData.js')
Page({
  data: {
    gradeData: [],
    refresh: false
  },
  onLoad(opt) {
    app.getUserInfo()
    this.getClassData()
    let user = wx.getStorageSync('userInfo')
    if (user) {
      getStuQuestionnaire(user.cluesOrStuId).then(res=>{
        let status = false
        res.quesSurveyList.forEach(item=>{
          // console.log(item)
          if (item.isReply == '0' && item.questionnaireState == '0'){
            status = true
          }
        })
        if (status){
          wx.showTabBarRedDot({
            index: 3
          })
        } else {
          wx.hideTabBarRedDot({
            index: 3
          })
        }
      })
    } else {
      wx.setStorageSync('share', {
        isShare: true,
        path: '../../grade/grade'
      })
      wx.reLaunch({
        url: '../login/login/login',
      })
    }

    // 请求信息栏有没有数据
    if (app.globalData.chooseNewsMsg) {
      util.getNewsMsg()
      app.globalData.chooseNewsMsg = false
    }

  },
  getClassData() {
    let _this = this
    wx.request({
      url: app.globalData.baseUrl + '/findMyClass',
      data: {
        stu_id: wx.getStorageSync("userInfo").cluesOrStuId
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        if (res.data.code == 0) {
          _this.solveData(res.data.data.myPackList)
        }
      }
    })
  },
  solveData(dataArr) {
    let lesson = []
    lesson = util.listSolve(dataArr)
    this.setData({
      gradeData: lesson
    })
    // getTime().then(res => {
      let num = '',
      baifenbi = '',
      that = this,
      currentDay = wx.getStorageSync('time'),
      gradeData = this.data.gradeData;
        for(let i=0;i<gradeData.length;i++)
        {
          if (gradeData[i].periods != null && gradeData[i].inPeriods != null) {
            num = gradeData[i].inPeriods / gradeData[i].periods
          } else {
            num = 0;
          }
          if (gradeData[i].status == 6) {
            if (currentDay >= gradeData[i].endtime) {
              baifenbi = 100
              // console.log(endtime)
            } else {
              if (num >= 1) {
                baifenbi = 100
              } else {
                baifenbi = (num * 100).toFixed(1)
              }
            }
          } else if (gradeData[i].status == 7) {
            // baifenbi = (num * 100).toFixed(1)
            baifenbi = 100
          } else {
            if (num >= 1) {
              baifenbi = 100
            } else {
              baifenbi = (num * 100).toFixed(1)
            }
          }
          var percent = 'gradeData['+ i+'].percent'
          that.setData({
            [percent]: baifenbi
          })
        }
    // })
  },
  // 下拉刷新
  onPullDownRefresh: function() {
    this.getClassData();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 500)
  },
  onShow() {
    app.globalData.stopMoreClick = false
    this.data.refresh && this.getClassData()
    this.setData({
      refresh: true
    })
  },
  /* 用户点击右上角分享*/
  onShareAppMessage: function () {
    return {
      title: '今天你认真学习了么？',//分享内容
      path: '/pages/grade/grade?isShare=1',//分享地址
      imageUrl: '/images/attendance/wdb_fx.png',//分享图片
    }
  }
})